package ttc.sftp.user;

import java.util.Vector;



import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.provider.SyncStateContract.Helpers;
import android.util.Log;



public class User {
	private String username;
	private String password;
	private String host;
	private String port;
	
	public String getUsername(){
		return username;
	}
	public String getPassword(){
		return password;
	}
	public String getHost(){
		return host;
	}
	public String getPort(){
		return port;
	}
	
	public void setUsername(String username){
		this.username = username;
	}
	public void setPassword(String password){
		this.password= password;
	}
	public void setPort(String port){
		this.port=port;
	}
	public void setHost(String host){
		this.host = host;
	}
	
	public User(String username,String password,String host,String port){
		this.username=username;
		this.password=password;
		this.host=host;
		this.port=port;
	}
	
	public User(String username,String password,String host,String port,Context context){
		this.username=username;
		this.password=password;
		this.host=host;
		this.port=port;
		
		SQLiteDatabase db;
		UserDatabaseHelper dbHelper;
		dbHelper = new UserDatabaseHelper(context);
		db= dbHelper.getWritableDatabase();
		
		ContentValues contentValues = new ContentValues();
		contentValues.put(UserDatabaseHelper.USERNAME, this.username);
		contentValues.put(UserDatabaseHelper.PASS, this.password);
		contentValues.put(UserDatabaseHelper.HOST, this.host);
		contentValues.put(UserDatabaseHelper.PORT, this.port);
		db.insert(UserDatabaseHelper.TABLE_ACCOUNT, null, contentValues);
		dbHelper.close();
	}
	public static void show(Context context){
		SQLiteDatabase db;
		UserDatabaseHelper dbHelper;
		dbHelper = new UserDatabaseHelper(context);
		db= dbHelper.getWritableDatabase();
		
		Cursor c = db.query(UserDatabaseHelper.TABLE_ACCOUNT, null, null, null, null, null, null);
		
		if (c.moveToFirst()) {
			do{
				Log.i("duy1", "User::"+c.getString(1));
				Log.i("duy1", "pass::"+c.getString(2));
			}while(c.moveToNext());
		}
		c.close();
		dbHelper.close();
	}
	
	public static void deleteAllRow(Context context){
		SQLiteDatabase db;
		UserDatabaseHelper dbHelper;
		dbHelper = new UserDatabaseHelper(context);
		db= dbHelper.getWritableDatabase();
		
		db.delete(UserDatabaseHelper.TABLE_ACCOUNT, null, null);
		dbHelper.close();
	}
	//delete by name
	public static void deleteByName(Context context,String user){
		SQLiteDatabase db;
		UserDatabaseHelper dbHelper;
		dbHelper = new UserDatabaseHelper(context);
		db = dbHelper.getReadableDatabase();
		db.delete(UserDatabaseHelper.TABLE_ACCOUNT, UserDatabaseHelper.USERNAME+"='"+user+"'",null);
		dbHelper.close();
	}
	
	
	public static Vector<User> getVectorUser(Context context){
		Vector<User> vectorUser= new Vector<User>();
		SQLiteDatabase db;
		UserDatabaseHelper dbHelper;
		dbHelper = new UserDatabaseHelper(context);
		db= dbHelper.getWritableDatabase();
		
		Cursor c = db.query(UserDatabaseHelper.TABLE_ACCOUNT, null, null, null, null, null, null);
		if(c.moveToFirst()){
			do{
				vectorUser.add(new User(c.getString(1), c.getString(2),c.getString(3),c.getString(4))); 
			}while(c.moveToNext());
		}
		c.close();
		dbHelper.close();
		return vectorUser;
		
	}
	
	

	
	
}
















